<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="姓名" prop="workerName">
        <el-input
          v-model="queryParams.workerName"
          placeholder="请输入人员姓名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <!--      <el-form-item label="年龄" prop="workerAge">-->
      <!--        <el-input-->
      <!--          v-model="queryParams.workerAge"-->
      <!--          placeholder="请输入年龄"-->
      <!--          clearable-->
      <!--          @keyup.enter.native="handleQuery"-->
      <!--        />-->
      <!--      </el-form-item>-->
      <el-form-item label="身份证号" prop="idCard">
        <el-input
          v-model="queryParams.idCard"
          placeholder="请输入身份证号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="手机号" prop="phone">
        <el-input
          v-model="queryParams.phone"
          placeholder="请输入手机号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <!--      <el-form-item label="现居住地址" prop="currentAddress">-->
      <!--        <el-input-->
      <!--          v-model="queryParams.currentAddress"-->
      <!--          placeholder="请输入现居住地址"-->
      <!--          clearable-->
      <!--          @keyup.enter.native="handleQuery"-->
      <!--        />-->
      <!--      </el-form-item>-->
      <!--      <el-form-item label="当日体温情况" prop="temperature">-->
      <!--        <el-input-->
      <!--          v-model="queryParams.temperature"-->
      <!--          placeholder="请输入当日体温情况"-->
      <!--          clearable-->
      <!--          @keyup.enter.native="handleQuery"-->
      <!--        />-->
      <!--      </el-form-item>-->
      <!--      <el-form-item label="身体健康情况(0健康 1异常)" prop="isHealth">-->
      <!--        <el-input-->
      <!--          v-model="queryParams.isHealth"-->
      <!--          placeholder="请输入身体健康情况(0健康 1异常)"-->
      <!--          clearable-->
      <!--          @keyup.enter.native="handleQuery"-->
      <!--        />-->
      <!--      </el-form-item>-->
      <!--      <el-form-item label="每日核酸检测结果(0阴性 1阳性)" prop="nucleicResult">-->
      <!--        <el-input-->
      <!--          v-model="queryParams.nucleicResult"-->
      <!--          placeholder="请输入每日核酸检测结果(0阴性 1阳性)"-->
      <!--          clearable-->
      <!--          @keyup.enter.native="handleQuery"-->
      <!--        />-->
      <!--      </el-form-item>-->
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['prevention:worker:add']"
        >新增
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['prevention:worker:edit']"
        >修改
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['prevention:worker:remove']"
        >删除
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['prevention:worker:export']"
        >导出
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="info"
          icon="el-icon-upload2"
          size="mini"
          @click="handleImport"
          v-has-permi="['prevention:worker:export']"
        >导入
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList" :columns="columns"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="workerList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column v-if="columns[0].visible" label="人员id" align="center" prop="workerId"/>
      <el-table-column v-if="columns[1].visible" label="人员姓名" align="center" prop="workerName"/>
      <el-table-column v-if="columns[2].visible" label="年龄" align="center" prop="workerAge"/>
      <el-table-column v-if="columns[3].visible" label="性别" align="center" prop="sex">
        <template slot-scope="scope">
          <span v-if="scope.row.sex==='0'">男</span>
          <span v-if="scope.row.sex==='1'">女</span>
        </template>
      </el-table-column>
      <el-table-column v-if="columns[4].visible" label="身份证号" align="center" prop="idCard"/>
      <el-table-column v-if="columns[5].visible" label="手机号" align="center" prop="phone"/>
      <el-table-column v-if="columns[6].visible" label="现居住地址" align="center" prop="currentAddress"/>
      <el-table-column v-if="columns[7].visible" label="当日体温情况" align="center" prop="temperature"/>
      <el-table-column v-if="columns[8].visible" label="健康情况" align="center" prop="isHealth">
        <template slot-scope="scope">
          <span v-if="scope.row.isHealth === '0'">健康</span>
          <span style="font-weight: bolder;color: red" v-if="scope.row.isHealth === '1'">异常</span>
        </template>
      </el-table-column>
      <el-table-column v-if="columns[9].visible" label="每日核酸检测结果" align="center" prop="nucleicResult">
        <template slot-scope="scope">
          <span v-if="scope.row.nucleicResult === '0'">阴性</span>
          <span style="font-weight: bolder;color: red" v-if="scope.row.nucleicResult === '1'">阳性</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['prevention:worker:edit']"
          >修改
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['prevention:worker:remove']"
          >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改检测人员信息对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="700px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="110px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="人员姓名" prop="workerName">
              <el-input v-model="form.workerName" placeholder="请输入人员姓名"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="年龄" prop="workerAge">
              <el-input v-model="form.workerAge" placeholder="请输入年龄"/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="身份证号" prop="idCard">
              <el-input v-model="form.idCard" placeholder="请输入身份证号"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="手机号" prop="phone">
              <el-input v-model="form.phone" placeholder="请输入手机号"/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="现居住地址" prop="currentAddress">
              <el-input v-model="form.currentAddress" placeholder="请输入现居住地址"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="当日体温情况" prop="temperature">
              <el-input v-model="form.temperature" placeholder="请输入当日体温情况"/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="身体健康情况" prop="isHealth">
              <el-select v-model="form.isHealth" placeholder="请选择健康状况">
                <el-option v-for="(item) in isHealthType" :key="item.type" :label="item.name"
                           :value="item.type"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="性别" prop="sex">
              <el-select v-model="form.sex" placeholder="请选择性别">
                <el-option  v-for="(item) in sexType" :key="item.type" :label="item.name" :value="item.type"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="核酸检测结果" prop="nucleicResult">
              <el-select v-model="form.nucleicResult" placeholder="请选择核酸结果">
                <el-option v-for="(item) in nucleicResultType" :key="item.type" :label="item.name"
                           :value="item.type"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
    <!-- 用户导入对话框 -->
    <el-dialog :title="upload.title" :visible.sync="upload.open" width="400px" append-to-body>
      <el-upload
        ref="upload"
        :limit="1"
        accept=".xlsx, .xls"
        :headers="upload.headers"
        :action="upload.url + '?updateSupport=' + upload.updateSupport"
        :disabled="upload.isUploading"
        :on-progress="handleFileUploadProgress"
        :on-success="handleFileSuccess"
        :auto-upload="false"
        drag
      >
        <i class="el-icon-upload"></i>
        <div class="el-upload__text">将文件拖到此处，或<em>点击上传</em></div>
        <div class="el-upload__tip text-center" slot="tip">
          <div class="el-upload__tip" slot="tip">
            <el-checkbox v-model="upload.updateSupport"/>
            是否更新已经存在的数据
          </div>
          <span>仅允许导入xls、xlsx格式文件。</span>
          <el-link type="primary" :underline="false" style="font-size:12px; vertical-align: baseline;"
                   @click="importTemplate">下载模板
          </el-link>
        </div>
      </el-upload>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitFileForm">确 定</el-button>
        <el-button @click="upload.open = false">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {listWorker, getWorker, delWorker, addWorker, updateWorker} from "@/api/prevention/worker";
import {getToken} from "@/utils/auth";

const sexType = [
  {type: '0', name: '男'},
  {type: '1', name: '女'},
]
const isHealthType = [
  {type: '0', name: '健康'},
  {type: '1', name: '异常'},
]
const nucleicResultType = [
  {type: '0', name: '阴性'},
  {type: '1', name: '阳性'},
]
export default {
  name: "Worker",
  data() {
    return {
      // 遮罩层
      loading: true,
      sexType,
      nucleicResultType,
      isHealthType,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 检测人员信息表格数据
      workerList: [],
      columns: [
        {key: 0, label: '人员id', visible: false},
        {key: 1, label: '人员姓名', visible: true},
        {key: 2, label: '年龄', visible: true},
        {key: 3, label: '性别', visible: true},
        {key: 4, label: '身份证号', visible: false},
        {key: 5, label: '手机号', visible: true},
        {key: 6, label: '现居住地址', visible: false},
        {key: 7, label: '当日体温情况', visible: true},
        {key: 8, label: '健康情况', visible: true},
        {key: 9, label: '每日核酸检测结果', visible: true},
      ],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      upload: {
        // 是否显示弹出层（用户导入）
        open: false,
        // 弹出层标题（用户导入）
        title: "",
        // 是否禁用上传
        isUploading: false,
        // 是否更新已经存在的用户数据
        updateSupport: 0,
        // 设置上传的请求头部
        headers: {Authorization: "Bearer " + getToken()},
        // 上传的地址
        url: process.env.VUE_APP_BASE_API + "/prevention/worker/importData"
      },
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        workerName: null,
        workerAge: null,
        sex: null,
        idCard: null,
        phone: null,
        currentAddress: null,
        temperature: null,
        isHealth: null,
        nucleicResult: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        workerName: [
          { required: true, message: "名称不能为空", trigger: "blur" },
          { min: 2, max: 20, message: '名称长度必须介于 2 和 20 之间', trigger: 'blur' }
        ],
        idCard: [
          { pattern: /^[1-9]\d{5}(18|19|20|(3\d))\d{2}((0[1-9])|(1[0-2]))(([0-2][1-9])|10|20|30|31)\d{3}[0-9Xx]$/,
            message: "请输入正确的身份证号码",
            trigger: "blur",
            required: true
          }
        ],
        phoneNum: [
          {
            pattern: /^1[3|4|5|6|7|8|9][0-9]\d{8}$/,
            message: "请输入正确的手机号码",
            trigger: "blur"
          }
        ]
      },
      filters: {
        sexTypeFilter(type) {
          const sexTy = sexType.find(obj => obj.type === type);
          return sexTy ? sexTy.name : null;
        },

        nucleicResultType(type) {
          const nucleicResultTy = nucleicResultType.find(obj => obj.type === type);
          return nucleicResultTy ? nucleicResultTy.name : null;
        },
        isHealthTypeFilter(type) {
          const isHealthTy = isHealthType.find(obj => obj.type === type);
          return isHealthTy ? isHealthTy.name : null;
        },
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询检测人员信息列表 */
    getList() {
      this.loading = true;
      listWorker(this.queryParams).then(response => {
        this.workerList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        workerId: null,
        workerName: null,
        workerAge: null,
        sex: null,
        idCard: null,
        phone: null,
        currentAddress: null,
        temperature: null,
        isHealth: null,
        nucleicResult: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.workerId)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加检测人员信息";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const workerId = row.workerId || this.ids
      getWorker(workerId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改检测人员信息";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.workerId != null) {
            updateWorker(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addWorker(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const workerIds = row.workerId || this.ids;
      this.$modal.confirm('是否确认删除检测人员信息编号为"' + workerIds + '"的数据项？').then(function () {
        return delWorker(workerIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('prevention/worker/export', {
        ...this.queryParams
      }, `worker_${new Date().getTime()}.xlsx`)
    },
    /** 导入按钮操作 */
    handleImport() {
      this.upload.title = "检测人员导入";
      this.upload.open = true;
    },
    /** 下载模板操作 */
    importTemplate() {
      this.download('prevention/worker/importTemplate', {}, `检测人员信息表_模板_${new Date().getTime()}.xlsx`)
    },

    // 文件上传中处理
    handleFileUploadProgress(event, file, fileList) {
      this.upload.isUploading = true;
    },
// 文件上传成功处理
    handleFileSuccess(response, file, fileList) {
      this.upload.open = false;
      this.upload.isUploading = false;
      this.$refs.upload.clearFiles();
      this.$alert(response.msg, "导入结果", {dangerouslyUseHTMLString: true});
      this.getList();
    },
// 提交上传文件
    submitFileForm() {
      this.$refs.upload.submit();
    },
  }
};
</script>
